package com.zzy.usefultool;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.base.Stopwatch;

public class GuavaTool {

	//前置条件
	@Test
	public  void checkSth() {
		//检查不为空
		//Double r = checkNotNull(null);
		//抛出异常 IllegalArgumentException
		int i=8;
		int j=7;
		checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);
		checkArgument(i < j, "Expected i < j, but %s > %s", i, j);
	}

	
	//前置条件
	@Test
	public  void objectUtils() {
		//比较
		Objects.equal("a", "a"); // returns true
		Objects.equal(null, "a"); // returns false
		Objects.equal("a", null); // returns false
		Objects.equal(null, null); // returns true
         //toString
		String str= Objects.toStringHelper(this).add("name", "yuexing")
                .add("id", "213")
                .add("pet", "xiaogou")
                .toString();
        System.out.println(str);
        //实现Comparable接口  //见Student
	}
	
	

	
	// 计时器
	@Test
	public void dosth() {
		Stopwatch stopwatch = Stopwatch.createStarted();
		// do something test
		for (int i = 0; i < 10000; i++) {
			System.out.println("----------");
		}
		long nanos = stopwatch.elapsed(TimeUnit.MICROSECONDS);
		Double time123=(double) nanos;
		
		stopwatch.stop();

		System.out.println(time123/1000000);
	}

	
	
	// 过滤字符 数字
	@Test
	public void dofilter() {
		String userInput = "nihao1234-1";
        CharMatcher ID_MATCHER = CharMatcher.DIGIT.or(CharMatcher.is('-'));
        System.out.println(ID_MATCHER.retainFrom(userInput));
	}

	

	@Test
	public void TestSplitter(){
		Iterable<String> strArray=Splitter.on(',').trimResults().omitEmptyStrings().split("foo, ,bar, quux,");
		Iterator<String> it=strArray.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}
	}	
	@Test
	public void TestJoiner(){
		Joiner joiner = Joiner.on("; ").skipNulls();
		System.out.println(joiner.join("Harry", null, "Ron", "Hermione"));
	}	
	
//	Splitter
//	String[] split = str.split(String regex);
//
//	Google Splitter的分隔符可以是单个的char，或者CharMatcher，或者字符串str，或者正则表达式的字符串patternStr，或者是一个Pattern，或者是固定length来分隔，分别有对应的 Factory 方法(on)来返回Splitter实例
//
//	另外还有trimResults和omitEmptyStrings来添加trim和去空操作。
//
//	最后，唯一的API，split(CharSequence seq)来split目标字符串(char序列)
//
//	Splitter.on(',').trimResults().omitEmptyStrings().split("foo, ,bar, quux,")
//	//["foo", "bar", "quux"]
	
	
//	Joiner
//	Java string中有Split没Join，所以经常使用StringBuilder进行append，Joiner即是很好的替代。
//
//	   Joiner joiner = Joiner.on("; ").skipNulls();
//	    . . .
//	   return joiner.join("Harry", null, "Ron", "Hermione");
//	CharMatcher
//	单个字符串的匹配器，看起来API很多，但在Introduction中介绍了其设计原则：
//
//	These represent a partial cross product of two notions:
//	(a) what's a "matching" character?
//	(b) what to do with those matching characters?
//
//	返回boolean类型的API用类匹配，返回int类型的API用来查找，返回String用来处理(retain, remove, replace, trim)
//
//	String seriesId = CharMatcher.DIGIT.or(CharMatcher.is('-')).retainFrom(input);
	

	
	
}
